import Store from '../store';
import { replaceHtml, transformRangeToAbsolute, openSelfModel } from '../utils/util';
import { modelHTML } from './constant';
import sheetmanage from './sheetmanage';
import menuButton from './menuButton';
import { checkProtectionNotEnable } from './protection';
import { jfrefreshgrid } from '../global/refresh';
import locale from '../locale/locale';
import { setcellvalue } from '../global/setdata';
import { getRangeAxis, getRangeWithFlatten, getRange, getTxtByRange } from '../global/api';
import { checkCellInRange, checkRangeCrossing } from "../utils/matrix";
import dataVerificationCtrl from './dataVerificationCtrl';
import luckysheetArray from '../global/array';


let isInitialSpecialMarkModel = false;
let specialMark
function initialSpecialMarkModel(title) {
    if (isInitialSpecialMarkModel) {
        return;
    }

    isInitialSpecialMarkModel = true;
    const _locale = locale();
    const locale_button = _locale.button;
    $("body").append(replaceHtml(modelHTML, {
        "id": "luckysheet-special-mark",
        "addclass": "luckysheet-special-mark",
        "title": "特殊标记",
        "content": `
        <div style="position:relative;">
            <table id="specialMarkId" class="cell-border stripe" >
            </table>
        </div>
        `,
        "botton": `<button id="luckysheet-specia-mark-confirm" class="btn btn-primary">${locale_button.confirm}</button>`,
        "style": "z-index:1011;width:500px;"
    }));

    // initialPermissionsViewModelEvent();
    $("#luckysheet-specia-mark-confirm").click(function () {
        if (!Store.drawSettingList) {
            Store.drawSettingList = []
        }
        let obj = {}
        Array.from(specialMark.data()).forEach(item => {
            let arr = item.cell.split('-');
            if (item.name == '名称') {
                obj.keyCell = arr[0]
                obj.keyContent = arr[1]
            } else if (item.name == '单位') {
                obj.unitCell = arr[0]
                obj.unitContent = arr[1]
            } else if (item.name == '值') {
                obj.valueCell = arr[0]
            }
        })
        if (obj.keyContent && obj.keyContent !== 'null' && obj.unitContent && obj.unitContent !== 'null') {
            Store.drawSettingList.push(obj);
            $("#luckysheet-special-mark").hide();
            $("#luckysheet-modal-dialog-mask").hide();
        } else {
            alert('特殊标记，名称和单位不能为空')
        }
    })

}
function crSelHtmlSpecialMark() {
    let valArr = [];
    let rang = []
    luckysheet.getRangeWithFlatten().forEach(item => {
        let mc = luckysheet.getCellValue(item.r, item.c, { type: 'mc' });
        if (mc) {
            if (mc.r == item.r && mc.c == item.c) {
                valArr.push(luckysheet.getCellValue(item.r, item.c) || '');
                rang.push({
                    row: [item.r, item.r],
                    column: [item.c, item.c]
                })
            }
        } else {
            valArr.push(luckysheet.getCellValue(item.r, item.c) || '');
            rang.push({
                row: [item.r, item.r],
                column: [item.c, item.c]
            })
        }


    });
    let cell = luckysheet.getTxtByRange(rang);
    let cellArr = cell.split(',');
    let option = '';
    for (let i = 0; i < cellArr.length; i++) {
        option += `<option value="${cellArr[i]}-${valArr[i]}">${cellArr[i]}/${valArr[i]}</option>`
    }
    return `<select >
            ${option}
        </select>`
}

function creatSpecialMark() {
    if (specialMark) {
        specialMark.destroy();
        $('#specialMarkId').empty();
    }
    let option = {
        searching: false,
        paging: false,
        "info": false,//左下角信息
        "language": { "sEmptyTable": "暂无数据", },
        scrollY: 150,
        "columns": [
            { title: '字段名称', data: "name", orderable: false, },
            { title: '位置/值', orderable: false, data: "cell" },
        ],
        columnDefs: [{
            "targets": [1],
            createdCell: function (cell, cellData, rowData, rowIndex, colIndex) {
                $(cell).html(crSelHtmlSpecialMark());
                var aInput = $(cell).find("select");
                aInput.val(cellData);
                $(cell).on("change", "select", function () {
                    let val = $(this).val()
                    specialMark.cell(cell).data(val);
                    $(cell).html(crSelHtmlSpecialMark());
                    var aInput = $(cell).find("select");
                    aInput.val(val);

                })

            }

        }],
        data: [
            {
                name: "名称",
                cell: ""
            }, {
                name: '值',
                cell: ""
            }, {
                name: "单位",
                cell: ""
            }
        ]

    }
    specialMark = $('#specialMarkId').DataTable(option);
}
// 
export function openSpecialMarkModel() {


    initialSpecialMarkModel();
    openSelfModel("luckysheet-special-mark");
    creatSpecialMark()


}